﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

using Automator.Runtime;
using Automator.Tasks.Core;

namespace Automator.Windows
{
    /// <summary>
    /// Interaction logic for NewTaskWindow.xaml
    /// </summary>
    public partial class NewTaskWindow : Window
    {
        public Task ResultTask { get; private set; }
        
        public NewTaskWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            ResultTask = null;
            RefreshTaskList();            
        }
        
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (lstTasks.SelectedItem != null)
            {
                ResultTask = lstTasks.SelectedItem as Task;
                this.Close();
            }
        }

        private void lstTasks_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstTasks.SelectedItem != null)
                BtnAdd_Click(sender, new RoutedEventArgs());
        }
        
        /// <summary>
        /// Rebuilds collection of available Tasks
        /// </summary>
        private void RefreshTaskList()
        {
            lstTasks.ItemsSource = GetTaskList();
            ICollectionView view = CollectionViewSource.GetDefaultView(lstTasks.ItemsSource);
            view.GroupDescriptions.Add(new PropertyGroupDescription("AssemblyFileName"));
        }
        
        /// <summary>
        /// Performs loading of all available Tasks into collection
        /// </summary>
        /// <returns>Read-only instance of the result collection</returns>
        private ReadOnlyTaskCollection GetTaskList()
        {
            TaskCollection tasks = new TaskCollection();
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            
            //loading built-in Tasks first
            TaskManager.GetAllTasksFromAssembly(Path.Combine(basePath, "Automator.Tasks.dll"), ref tasks);
            
            //scan addins directory
            if (Directory.Exists(Automator.Runtime.Environment.addinsDir))
            {
                IEnumerable<string> addins = Directory.EnumerateFiles(Automator.Runtime.Environment.addinsDir, "*.dll", SearchOption.AllDirectories);
                foreach (string addin in addins)
                    TaskManager.GetAllTasksFromAssembly(addin, ref tasks);
            }

            return new ReadOnlyTaskCollection(tasks);
        }
        
        private bool Filter(object value)
        {
            Task task = value as Task;
            if (value == null) return false;
            
            if (String.IsNullOrEmpty(edtFilter.Text))
                return true;
            else
                return task.Title.ToLower().Contains(edtFilter.Text.ToLower());
        }
        
        private void EdtFilter_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            ICollectionView view = CollectionViewSource.GetDefaultView(lstTasks.ItemsSource);
            view.Filter = null;
            view.Filter = new Predicate<object>(Filter);
        }
    }
}
